package de.justphil.tcg.tcgserver.commons.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import de.justphil.tcg.tcgserver.commons.util.DateTimeUtil;

@Entity
@Table(name = "cards", schema = "tcg")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Card implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2650303194356309458L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "card_id")
	@XmlElement
	private long id;
	
	@Column(name = "name")
	@XmlElement
	private String name;
	
	@Column(name = "life")
	@XmlElement
	private int life;
	
	@Column(name = "attack")
	@XmlElement
	private int attack;
	
	@Column(name = "defense")
	@XmlElement
	private int defense;
	
	@Column(name = "agility")
	@XmlElement
	private int agility;
	
	@OneToMany(fetch=FetchType.EAGER)
    @JoinColumn(name="card_id", referencedColumnName="card_id")
    private Set<CardInstance> cardInstances;
	
	@XmlElement
	@JoinTable(name = "card_properties", joinColumns = {
	        @JoinColumn(name = "card_id", referencedColumnName = "card_id")}, inverseJoinColumns = {
	        @JoinColumn(name = "property_id", referencedColumnName = "property_id")})
    @ManyToMany(fetch=FetchType.EAGER)
    private Set<Property> properties;
	
	@Column(name = "created")
	@Temporal(TemporalType.TIMESTAMP)
	private Date created;
	
	@Column(name = "modified")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modified;
	
	
	
	public Card() {
		// JPA requires a default constructor without parameters
		
		this.cardInstances = new HashSet<CardInstance>();
		this.properties = new HashSet<Property>();
	}
	
	public Card(String name, int life, int attack, int defense, int agility) {
		this();
		
		this.name = name;
		this.life = life;
		this.attack = attack;
		this.defense = defense;
		this.agility = agility;
		
		long nowUTC = DateTimeUtil.getUTCTimeInMillis();
		this.created = new Date(nowUTC);
		this.modified = new Date(nowUTC);
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	public long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public int getLife() {
		return life;
	}

	public int getAttack() {
		return attack;
	}

	public int getDefense() {
		return defense;
	}

	public int getAgility() {
		return agility;
	}

	public Date getCreated() {
		return created;
	}

	public Date getModified() {
		return modified;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public void setAttack(int attack) {
		this.attack = attack;
	}

	public void setDefense(int defense) {
		this.defense = defense;
	}

	public void setAgility(int agility) {
		this.agility = agility;
	}

	public Set<CardInstance> getCardInstances() {
		return cardInstances;
	}

	public Set<Property> getProperties() {
		return properties;
	}

	public void setCardInstances(Set<CardInstance> cardInstances) {
		this.cardInstances = cardInstances;
	}

	public void setProperties(Set<Property> properties) {
		this.properties = properties;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + agility;
		result = prime * result + attack;
		result = prime * result + defense;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + life;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Card other = (Card) obj;
		if (agility != other.agility)
			return false;
		if (attack != other.attack)
			return false;
		if (defense != other.defense)
			return false;
		if (id != other.id)
			return false;
		if (life != other.life)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	/*
	@Override
	public void readData(DataInput in) throws IOException {
		id = in.readLong();
		name = in.readUTF();
		life = in.readInt();
		attack = in.readInt();
		defense = in.readInt();
		agility = in.readInt();
		created = new Date(in.readLong());
		modified = new Date(in.readLong());
	}

	@Override
	public void writeData(DataOutput out) throws IOException {
		out.writeLong(id);
		out.writeUTF(name);
		out.writeInt(life);
		out.writeInt(attack);
		out.writeInt(defense);
		out.writeInt(agility);
		out.writeLong(created.getTime());
		out.writeLong(modified.getTime());
	}
	*/
	
	/* ######################################################################## */
	/* ######################################################################## */
	/* ######################################################################## */
	
	public long[] getPropertyListAsArray() {
		long[] out = new long[properties.size()];
		
		int i = 0;
		for (Property p : properties) {
			out[i] = p.getId();
			i++;
		}
		
		return out;
	}
	
	/* ######################################################################## */
	/* ######################################################################## */
	/* ######################################################################## */
	

	public class Name {
		public static final String BIGFOOT			= "Bigfoot";
		public static final String BULLFROG			= "Bullfrog";
		public static final String CERBERUS			= "Cerberus";
		public static final String ELEPHANT			= "Elephant";
		public static final String ELF_MAGE			= "Elf Mage";
		public static final String GOBLIN			= "Goblin";
		public static final String HEDGEHOG			= "Hedgehog";
		public static final String MAGIC_ROCK		= "Magic Rock";
		public static final String ROADRUNNER		= "Roadrunner";
		public static final String SNAKE			= "Snake";
		public static final String WILD_BORE		= "Wild Bore";
		public static final String WOLF				= "Wolf";
		
	}
}
